package com.ukao.steward.ui.valetRunners.storage;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseActivity;
import com.ukao.steward.bean.ProductRelImgBean;
import com.ukao.steward.listener.MyOnPageChangeListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.ukao.steward.consts.Constant.ARG_PARAM1;
import static com.ukao.steward.consts.Constant.ARG_PARAM2;

public class PhotoListActivity extends BaseActivity {


    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.index_tv)
    TextView indexTv;
    private ArrayList<ProductRelImgBean> urls;
    private int pagerPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        setContentView(R.layout.activity_photo_list);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void initView() {
        urls = getIntent().getParcelableArrayListExtra(ARG_PARAM1);
        pagerPosition = getIntent().getIntExtra(ARG_PARAM2, 0);// 获得位置
        ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(urls.size() - 1);
        mViewPager.setCurrentItem(pagerPosition, false);
        mViewPager.addOnPageChangeListener(new MyOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
               int  index=position+1;
                indexTv.setText(index + "/" + urls.size());

            }
        });

    }

    @OnClick({R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.index_tv:
                break;
        }
    }

    private class ImagePagerAdapter extends FragmentPagerAdapter {

        public ArrayList<ProductRelImgBean> fileList;

        public ImagePagerAdapter(FragmentManager fm, ArrayList<ProductRelImgBean> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }


        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position).getCameraImg();
            return PhotoDetailFragment.newInstance(url, "");
        }

    }
}
